package com.example.nsl


import android.animation.ObjectAnimator
import android.app.Activity
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.*
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.nsl.R.id
import com.example.nsl.R.layout


class AdImageActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.test)
//        setContentView(R.layout.activity_main)
        setContentView(layout.test2)
        initLayout()
        window.statusBarColor=Color.TRANSPARENT

        // First apply the insets listener

        findViewById<View>(R.id.udl).setOnClickListener {
            var url=Uri.parse("https://wx.cut.iredot.com/cut-course/index.html?material=1#/pages/index/index?material=1")

            url.getQueryParameter("material")
            Log.e("Inof",url.path+"")
            Log.e("Inof",url.fragment +"")
        }
        val helpView=HelpView(this)
        val rootView=getRootFrame(this)
        val layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams.gravity=Gravity.RIGHT or Gravity.BOTTOM
        layoutParams.bottomMargin=121
        rootView.addView(helpView,layoutParams)
        helpView.setBackgroundColor(Color.BLUE)
        //        LayoutParams layoutParams=   new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//       addView(view,layoutParams);
//        helpView.setGravity()


        SoftKeyboardUtils(this).apply {
            setOnSoftKeyboardChangeListener(object :SoftKeyboardUtils.OnSoftKeyboardChangeListener{
                override fun onKeyboardShow(height: Int) {

                    helpView.translationY= -height.toFloat()
                }

                override fun onKeyboardHide(height: Int) {
                    helpView.translationY=0f

                }
            })
        }
    }


    fun testCode(){
        val rootView=getRootFrame(this)
        val viewHelp=LayoutInflater.from(this).inflate(R.layout.helpview,rootView,true)
        val helpview=viewHelp.findViewById<LinearLayout>(R.id.layoutHelp)
        val helpIamge=viewHelp.findViewById<ImageView>(R.id.imageview)
        val textView= (viewHelp as FrameLayout).findViewById<TextView>(R.id.textView)

        helpIamge.setOnClickListener {
            if(helpview.translationX==0f){
                val objectAnimator: ObjectAnimator = ObjectAnimator.ofFloat(helpview, "translationX", 0f,textView.measuredWidth.toFloat())
                objectAnimator.setDuration(200)
                objectAnimator.start()
            }else{
                val objectAnimator: ObjectAnimator = ObjectAnimator.ofFloat(helpview, "translationX", textView.measuredWidth.toFloat(), 0f)
                objectAnimator.setDuration(200)
                objectAnimator.start()
            }

        }
//        rootView.addView()

        Log.e("AdImageActivity","1 ${this::class.java.name}")


        if(textView.measuredWidth>0){
            Log.e("AdImageActivity","2  ${textView.measuredWidth}")
//            viewHelp.translationX= (-textView.measuredWidth).toFloat();
            helpview.visibility=View.VISIBLE
        }else{
            textView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
                override fun onGlobalLayout() {
                    Log.e("AdImageActivity","3 ${textView.measuredWidth}")
                    Log.e("AdImageActivity","viewHelp ${viewHelp}")
                    textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
                    helpview.translationX= (textView.measuredWidth).toFloat();
                    helpview.visibility=View.VISIBLE
                }

            })
        }

    }


    override fun onUserInteraction() {
        super.onUserInteraction()
    }

    override fun onUserLeaveHint() {
        super.onUserLeaveHint()
    }



    fun getRootFrame(activity: Activity): FrameLayout {
        var re: View = activity.findViewById(android.R.id.content)
        if (re != null && re is FrameLayout) {
            return re as FrameLayout
        }
        val viewGroup = activity.getWindow().getDecorView() as ViewGroup
        re = viewGroup.getChildAt(viewGroup.childCount - 1)
        return if (re != null && re is FrameLayout) {
            re as FrameLayout
        } else {
            re = FrameLayout(activity)
            activity.getActionBar()?.getHeight()
            val lp = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            viewGroup.addView(re, lp)
            re as FrameLayout
        }
    }
    fun initLayout() {
        val recyclerView=findViewById<RecyclerView>(id.recyclerview)
        recyclerView.layoutManager= LinearLayoutManager(this).apply {
            orientation=LinearLayoutManager.VERTICAL
            this
        }
        recyclerView.adapter=object :RecyclerView.Adapter<RecyclerView.ViewHolder>(){
            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): RecyclerView.ViewHolder {
                return  CustomViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false))
            }

            override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                if(position%2==0){
                    holder.itemView.setBackgroundColor(Color.RED)
                }else{
                    holder.itemView.setBackgroundColor(Color.BLUE)
                }
                if(position==4){
//                    holder.itemView.setBackgroundColor(Color.TRANSPARENT)
                    holder.itemView.setBackgroundResource(R.drawable.shape_image)
                }
            }

            override fun getItemCount(): Int {
                 return 9;
            }

            //自定义ViewHolder
            inner class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

            }
        }
//        ViewCompat.setOnApplyWindowInsetsListener(recyclerView
//        ) { _, insets -> insets!! }

//        layoutroot.rotation=-180f
    }





}